
package org.sidora.core;

import java.io.Serializable;
import java.text.NumberFormat;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Predicate;
import java.util.logging.Level;
import org.sidora.core.util.AbstractFactory;
import org.sidora.core.util.AbstractInventory;
import org.sidora.core.util.LoggerProvider;
import org.sidora.core.util.OCProvider;
import org.sidora.core.util.ParameterCollector;
//import com.vividsolutions.jts.geom.Geometry;


/**
 * Classe que representa un jaciment arqueologic
 * @author Enric Tartera, Juan Manuel Gimeno, Anna Arnalot, Roger Masgoret
 * @version 1.0
 */
public class Site implements Comparable, Serializable{
    
    private String id ;
    
    private String acronyn;
    private String name;
    private String abbreviation;
    
    private String toponym;
    private String town;
    private String area;
    private String region;
    private String country;
    private String description;
    private Action defaultAction;

    
    private Site(){	}
    
    
    @Override
    public boolean equals(Object obj) {
        if (obj == this)	return true;
        if (!(obj instanceof Site)) return false;
        Site site = (Site)obj;
        if (this.getId() != site.getId()) return false;
        return true;
    }
    
    @Override
    public String toString() {
        
        return getName()+" ("+getId()+")";
    }
    
    
//
//    static class Factory extends AbstractFactory{
//
//        protected Object create(ParameterCollector param) {
//            // La pol�tica de creaci� de jaciments determina que nom�s un usuari
//            //amb rol "admin" pot crear nous jaciments.
//            Site newSite = new Site();
//            return newSite;
//
//
//        }
//    }
    public static class Inventory extends AbstractInventory{
        
        public boolean exist(final String id) throws Exception{
            
            ObjectContainer db = OCProvider.getInstance();
            ObjectSet<Site> sites = db.query(new Predicate<Site>() {
                public boolean match(Site candidate) {
                    return candidate.getId().equals(id);
                }
            });
            
            if (sites.hasNext()){
                return true;
            } else {
                return false;
            }
        }
        
        public Site getNewObject(ParameterCollector param) throws Exception{
            
            String newId= (String) param.get("siteid");
            if (!exist(newId)){
                LoggerProvider.getInstance().info("New Site created");
                return new Site();
            } else {
                LoggerProvider.getInstance().info("Existing Site returned: "+newId);
                return (Site) getObject(param);
            }
        }
        
        
        public static String getNewKey() {
            ObjectContainer db = OCProvider.getInstance();
            ObjectSet <Site> sites = db.query(Site.class);
            SortedSet <Integer>sorted = new TreeSet<Integer>();
            
            NumberFormat form = NumberFormat.getInstance();
            form.setMinimumIntegerDigits(3);
            
            if (sites.size()!=0){
                while (sites.hasNext()){
                    Site asite = sites.next();
                    try{
                        Integer integer = Integer.parseInt(asite.getId());
                        sorted.add(integer);
                    }catch (NumberFormatException e){
                        LoggerProvider.getInstance().log(Level.WARNING,"Not an integer",e);
                    }
                }
                int last = sorted.last().intValue();
                return form.format(last+1);
            } else return "001";
            
            
        }
        public KeyObject getNewKey(ParameterCollector param) {
            return null;
        }
        
        public Object getObject(ParameterCollector param) {
            //Carrega el Site amb el corresponent siteid
            
            Site site = null;
            final String key = (String) param.get("siteid");
            ObjectContainer db = OCProvider.getInstance();
            ObjectSet <Site> sites = db.query(new Predicate <Site>() {
                public boolean match(Site asite) {
                    return (asite.getId().equals(key));
                }
            });
            
            
            if (sites.hasNext()){
                site = sites.next();
                return site;
            } else{
                LoggerProvider.getInstance().warning("Unknow Site");
                return null;
            }
        }
        public static List getList() {
            
            ObjectContainer db = OCProvider.getInstance();
            List<Site> list = db.query(Site.class);
            
            return list;
            
        }
        
    }//End Inventori
    public int compareTo(Object obj) {
        
        return this.toString().compareTo(obj.toString());
    }
    
    public TreeSet<Describer> getTypes() {
        return new TreeSet<Describer>(Describer.Inventory.getList(this));
    }
    
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getAcronym() {
        return acronyn;
    }
    
    public void setAcronyn(String acronyn) {
        this.acronyn = acronyn;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getAbbreviation() {
        return abbreviation;
    }
    
    public void setAbbreviation(String abbreviation) {
        this.abbreviation = abbreviation;
    }
    
    public String getToponym() {
        return toponym;
    }
    
    public void setToponym(String toponym) {
        this.toponym = toponym;
    }
    
    public String getTown() {
        return town;
    }
    
    public void setTown(String town) {
        this.town = town;
    }
    
    public String getArea() {
        return area;
    }
    
    public void setArea(String area) {
        this.area = area;
    }
    
    public String getRegion() {
        return region;
    }
    
    public void setRegion(String region) {
        this.region = region;
    }
    
    public String getCountry() {
        return country;
    }
    
    public void setCountry(String country) {
        this.country = country;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }

    public Action getDefaultAction() {
        return defaultAction;
    }

    public void setDefaultAction(Action defaultAction) {
        this.defaultAction = defaultAction;
    }
    
} // end Site



